<?php
namespace app\index\controller;

use think\Request;
use think\Db;
use think\Config;
class Index extends \think\Controller
{
    /**
     * 主页
     */
    public function index()
    {

        return $this -> fetch('index');
    }

    /**
     * 行业管理页面
     */
    public function business(){

        // 是否为 POST 请求
        if (Request::instance()->isPost()){
            $postArr = $_POST;
            $businessName = strFilter($postArr['businessName']);

            $insertData = array(
                'business_name' => $businessName,
                'add_time' => time()
            );
            $res = Db::name('business') -> insert($insertData);
            if($res){
                $ajax['status'] = 1;
                $ajax['msg'] = '添加行业成功';
                return ($ajax);
                exit();
            }else{
                $ajax['status'] = 0;
                $ajax['msg'] = '添加行业失败';
                return ($ajax);
                exit();
            }
        }
        //普通请求
        $busData = Db::name('business') -> where('is_delete',0) ->order('id desc')->paginate(5);
        $page = $busData -> render();

        $this -> assign('busData',$busData);
        $this -> assign('page',$page);
        return $this -> fetch('business');
    }

    /*
     *  方法-删除行业
     */
    public function _delBusiness(){
        // 是否为 POST 请求
        if (Request::instance()->isPost()){
            if(isset($_POST['busId'])){
                $postArr = $_POST;
                $busId = strFilter($postArr['busId']);

                $busArr = array(
                    'is_delete' => 1
                );
                $res = Db::name('business') -> where('id',$busId) -> update($busArr);
                if($res){
                    $ajax['status'] = 1;
                    $ajax['msg'] = '删除成功';
                    return $ajax;
                }else{
                    $ajax['status'] = 0;
                    $ajax['msg'] = '删除失败';
                    return $ajax;
                }
            }
        }
    }

    /*
     * 方法-关键词名称
     */


    /**
     * 上传页面
     */
    public function upload(){
        return $this -> fetch('upload');
    }


    /**
     * 关键词页面
     */
    public function keywords(){

        // 是否为 POST 请求
        if (Request::instance()->isPost()){

            if($_POST['businessType'] != 0 && $_POST['businessName']!= '' ){
                $postArr = $_POST;
                $insertData = array(
                    'bid' => strFilter($postArr['businessType']),
                    'keyword' => strFilter($postArr['businessName']),
                    'add_time' => time()
                );

                $res = Db::name('keywords') -> insert($insertData);
                if($res){
                    $this -> success('关键词添加成功');
                }else{
                    $this -> error('关键词添加失败');
                }
            }else{
                $this -> error('参数不能为空');
            }
        }
        //查询行业名称
        $busTitle = Db::name('business') -> where('is_delete',0) ->field('id,business_name') -> select();

        //关键词列表
        $keywordsData = Db::name('keywords') -> where('is_delete',0) -> field('id,keyword,bid,add_time,select_state,upload_time') -> where('is_delete',0) -> paginate(10);
        $page = $keywordsData -> render();

        $this -> assign('keywordsData',$keywordsData);
        $this -> assign('page',$page);
        $this -> assign('busTitle',$busTitle);
        return $this -> fetch('keywords');
    }


    /**
     * 网站管理页面
     */
    public function website(){

        //查询website表的数据
        $websiteData = Db::name('website') -> where('is_delete',0) ->order('id desc') -> paginate(100);
        $page = $websiteData -> render();

        $this -> assign('websiteData',$websiteData);
        $this -> assign('page',$page);
        return $this -> fetch('website');
    }


    /**
     * 资讯管理页面
     */
    public function newsinfo(){

        //条件数组
        $whereNewsArticle = array(
            'is_delete' => 0,
        );
        //查询文章列表数据
        $articleData = Db::name('newsinfo_article') -> where($whereNewsArticle) ->order('id desc') -> paginate(20);
        $page = $articleData -> render();


        $this -> assign('articleData',$articleData);
        $this -> assign('page',$page);

        return $this -> fetch('newsinfo');
    }

    /*
     *  删掉文章数据
     */
    public function _delArticle(){
        // 是否为 POST 请求
        if (Request::instance()->isPOST()) {
            $postArr = $_POST;

            //判断提交参数
            if($postArr['id'] == ''){
                $ajax['status'] = 0;
                $ajax['msg'] = '参数不能为空';
                echo json_encode($ajax);
                exit();
            }

            $updateData = array(
                'is_delete' => 1
            );
            //进行删除
            $resClass = Db::name('newsinfo_article') -> where('id',$postArr['id']) -> update($updateData);
            if($resClass){
                $ajax['status'] = 1;
                $ajax['msg'] = '文章删除成功';
                echo json_encode($ajax);
                exit();
            }else{
                $ajax['status'] = 2;
                $ajax['msg'] = '文章删除失败';
                echo json_encode($ajax);
                exit();
            }
        }
    }

    /**
     * 资讯管理创建
     */
    public function newscreate(){

        // 是否为 POST 请求
        if (Request::instance()->isPOST()) {
            $postArr = $_POST;

            //判断提交参数
            if($postArr['title'] == '' || $postArr['type'] == ''|| $postArr['is_banner'] == ''|| $postArr['content'] == '' || $postArr['classType'] == '' || $postArr['classType'] == 0 || sizeof($postArr['coverImgArr']) == 0){
                $ajax['status'] = 0;
                $ajax['msg'] = '参数为空';
                echo json_encode($ajax);
                exit();
            }
            //插入文章
            $insertArticle = array(
                'title' => $postArr['title'],
                'type' => $postArr['type'],
                'content' => $postArr['content'],
                'is_banner' => $postArr['is_banner'],
                'cid' => $postArr['classType'],
                'add_time' => time()
            );
            $resArticle = Db::name('newsinfo_article') -> insert($insertArticle);
            $lastId = Db::name('newsinfo_article') -> getLastInsID();
            //插入封面
            if($postArr['coverImgArr']){
                foreach($postArr['coverImgArr'] as $k => $v){
                    $insertCoverImg = array(
                        'img' => Config::get('serverUrl').$v,
                        'aid' => $lastId,
                        'add_time' => time()
                    );
                    $resCover = Db::name('newsinfo_image') -> insert($insertCoverImg);
                }
            }

            if($resArticle){
                $ajax['status'] = 1;
                $ajax['msg'] = '创建成功';
                echo json_encode($ajax);
                exit();
            }else{
                $ajax['status'] = 0;
                $ajax['msg'] = '创建失败';
                echo json_encode($ajax);
                exit();
            }

        }
        //选择发布类别
        $classDataArr = Db::name('newsinfo_class') -> where('is_delete',0) ->field('id,classname') -> select();

        $this -> assign('classDataArr',$classDataArr);
        return $this -> fetch('newscreate');
    }

    /**
     * 资讯类别管理页面
     */
    public function newstitle(){

        // 是否为 POST 请求
        if (Request::instance()->isPOST()) {
            $postArr = $_POST;

            if($postArr['classType'] == ''){
                $ajax['status'] = 0;
                $ajax['msg'] = '类别名称不能为空';
                echo json_encode($ajax);
                exit();
            }

            //判断是否已经存在
            $resClassNum = Db::name('newsinfo_class') -> where('classname',$postArr['classType']) -> count();
            if($resClassNum > 0){
                $ajax['status'] = 2;
                $ajax['msg'] = '类别名称已存在';
                echo json_encode($ajax);
                exit();
            }

            $insertClass = array(
                'classname' => $postArr['classType'],
                'add_time' => time()
            );

            $res = Db::name('newsinfo_class') -> insert($insertClass);
            if($res){
                $ajax['status'] = 1;
                $ajax['msg'] = '创建成功';
                echo json_encode($ajax);
                exit();
            }else{
                $ajax['status'] = 3;
                $ajax['msg'] = '创建失败';
                echo json_encode($ajax);
                exit();
            }
        }

        $whereTitle = array(
            'is_delete' => 0
        );
        $classData = Db::name('newsinfo_class') -> where($whereTitle) -> order('id desc') -> paginate(20);
        $page = $classData -> render();


        $this -> assign('classData',$classData);
        $this -> assign('page',$page);

        return $this -> fetch('newstitle');
    }

    /*
     *  删掉类别名称
     */
    public function _delClassName(){
        // 是否为 POST 请求
        if (Request::instance()->isPOST()) {
            $postArr = $_POST;

            //判断提交参数
            if($postArr['id'] == ''){
                $ajax['status'] = 0;
                $ajax['msg'] = '参数不能为空';
                echo json_encode($ajax);
                exit();
            }

            $updateData = array(
                'is_delete' => 1
            );
            //进行删除
            $resClass = Db::name('newsinfo_class') -> where('id',$postArr['id']) -> update($updateData);
            if($resClass){
                $ajax['status'] = 1;
                $ajax['msg'] = '类别名称删除成功';
                echo json_encode($ajax);
                exit();
            }else{
                $ajax['status'] = 2;
                $ajax['msg'] = '类别名称删除失败';
                echo json_encode($ajax);
                exit();
            }
        }
    }

}
